package com.cloudoer.blog.controller;

import com.cloudoer.blog.base.BaseController;
import com.cloudoer.blog.model.Album;
import com.cloudoer.blog.model.Member;
import com.cloudoer.blog.model.info.AlbumInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/album")
public class AlbumController extends BaseController {

    @RequestMapping(value = "/index")
    public String index (){
        return "album/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add (ModelMap modelMap){
        modelMap.put("action", "/album/add");
        modelMap.put("album", new Album());
        return "album/add";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update (String id, ModelMap modelMap){
        modelMap.put("action", "/album/update");
        modelMap.put("album", albumService.getById(id));
        return "album/add";
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ResponseBody
    public Album getById (String id){
        return albumService.getById(id);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map getByPage (@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<Album> list = albumService.getByPage((pno-1)*psize, psize);
        return result(list, albumService.getCount(), "");
    }

    @RequestMapping(value = "/getByRanking", method = RequestMethod.POST)
    @ResponseBody
    public Map getByRanking (){
        List<Album> list = albumService.getByRanking();
        return result(list, albumService.getCount(), "");
    }

    @RequestMapping(value = "/getByPageInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map getByPageInfo (@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<AlbumInfo> list = albumService.getByPageInfo((pno-1)*psize, psize);
        return result(list, albumService.getCount(), "");
    }

    @RequestMapping(value = "/getByRankingInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map getByRankingInfo (){
        List<AlbumInfo> list = albumService.getByRankingInfo();
        return result(list, albumService.getCount(), "");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map add(Album album){
        albumService.add(album);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map update(Album album){
        albumService.update(album);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map delete(String id){
        albumService.delete(id);
        fileUtil.deleteImg(id);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/endorse", method = RequestMethod.POST)
    @ResponseBody
    public Map endorse(String id){
        albumService.endorse(id);
        return result(SUCCESS, "操作成功");
    }
}
